package day14_io.作业14;

import java.io.*;

//4.复制文件夹E:\csw\Demo\csw1下面所有文件和子文件夹内容到E:\csw\Demo\copy。
//提示：涉及单个文件复制、目录的创建、递归的使用
public class 作业4copycopy {
    public static void main(String[] args) throws Exception{
        File file = new File("E:\\csw\\Demo\\csw1");
        File newfile = new File("E:\\csw\\Demo\\copy");
        show(file,newfile);
    }
    public static void show(File file,File newfile) throws Exception{
        File[] files = file.listFiles();
        for (File file1 : files) {if (file1.isFile()){
            String s=newfile.getAbsolutePath()+"\\"+file1.getName();
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1.getAbsolutePath()));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(s));
            byte[] bytes = new byte[1024];
            int i;
            while ((i=bis.read(bytes))!=-1){
                bos.write(bytes,0,i);
            }
            bis.close();
            bos.close();
        }else{
            File file2 = new File(newfile, file1.getName());
            file2.mkdirs();
            show(file1,file2);
        }
        }
//        for (File file1 : files) {
//            if (file1.isFile()){
//                File file2 = new File(newfile,file1.getName());
//                file2.createNewFile();
//            }else{
//                File file2 = new File(newfile,file1.getName());
//                file2.mkdirs();
//                show(file1,file2);
//            }
//        }
    }
}
